package com.ruoyi.common.core.web.page;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils;

import java.util.Map;

/**
 * 表格数据处理
 * 
 * @author ruoyi
 */
public class TableSupport
{
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "current";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "sorter";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 分页参数合理化
     */
    public static final String REASONABLE = "reasonable";

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain()
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(ServletUtils.getParameterToInt(PAGE_NUM));
        pageDomain.setPageSize(ServletUtils.getParameterToInt(PAGE_SIZE));
        String jsString = ServletUtils.getParameter(ORDER_BY_COLUMN);
        if (!StringUtils.isEmpty(jsString)) {
            JSONObject jsonObject = JSONObject.parseObject(jsString);
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                pageDomain.setOrderByColumn(entry.getKey());
                if (entry.getValue().equals("asc")) {
                    pageDomain.setIsAsc("asc");
                } else {
                    pageDomain.setIsAsc("desc");
                }
            }
        }

        pageDomain.setReasonable(ServletUtils.getParameterToBool(REASONABLE));
        return pageDomain;
    }

    public static PageDomain buildPageRequest()
    {
        return getPageDomain();
    }
}
